package com.z.other.controller;


import com.z.base.PageTableRequest;
import com.z.base.ResponseCode;
import com.z.base.Results;
import com.z.other.service.IFilesService;
import com.z.other.model.Files;
import com.z.other.utils.KitFileUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxy
 * @since 2022-03-07
 */
@Controller
@RequestMapping("/other/files")
@Slf4j
public class FilesController {
    
    @Autowired
    private IFilesService filesService;


    @GetMapping("listByMap")
    @ApiOperation("文件列表分页条件查询")
    @ResponseBody
    public Results listByMap(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();

        Map<String, Object> map = new HashMap<>();
        String name = request.getParameter("name");
        if(!StringUtils.isEmpty(name))
            map.put("name_like_s", name);

        Results<Files> results = filesService.findPageByMap(map, page.getOffset(), page.getLimit());
        return results;
    }

    @GetMapping("toList")
    @ApiOperation("文件列表页面")
    public String toList() {
        return "other/files/filesList";
    }

    @GetMapping("toUploadPage")
    @ApiOperation("文件上传页面")
    public String toUploadPage() {
        return "other/files/filesUpload";
    }

    @PostMapping(value = "/uploadVideo")
    @ApiOperation("文件上传接口")
    @ResponseBody
    public Object uploadVideo(@RequestParam("layuiVideo") MultipartFile[] layuiFile, HttpServletRequest request, HttpServletResponse response){
        log.info("进入wph的layui视频上传接口》》》》》》》》》》》》》》》");
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        KitFileUtil kitFileUtil=new KitFileUtil();
        map = kitFileUtil.kitFileUtil(layuiFile, request, response);
        String error = map.get("error").toString();
        if("101".equals(error)){
            map2.put("error",101);
            map2.put("message",map.get("message"));
            return map;
        }
        map2.put("error",0);
        String url[]=(String[])map.get("url");
        map2.put("url",url[0]);
        return Results.success(map2);
    }
    @ApiOperation("文件添加页面")
    @GetMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("sysUser", new Files());
        return "other/files/filesAdd";
    }

    @ApiOperation("文件修改页面")
    @GetMapping("toEdit")
    public String toEdit(Model model, String id) {
        if(StringUtils.isEmpty(id)){
            return "参数异常";
        }
        model.addAttribute("sysUser", filesService.findById(id));
        return "other/files/filesEdit";
    }

    //跳转预览页面
    @RequestMapping(value = "/goLook", method = RequestMethod.GET)
    public Object goLook(HttpServletRequest request, HttpSession session){
        log.info("课堂管理 》预览");

        ModelAndView mv = new ModelAndView();
        String classVideo = request.getParameter("classVideo");
        mv.addObject("kitG", classVideo);
        mv.setViewName("other/files/yulan");
        return mv;
    }

    @ApiOperation("文件添加接口")
    @PostMapping("add")
    @ResponseBody
    public Results add(Files files, Integer roleId){
        try{
            filesService.save(files);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @ApiOperation("文件修改接口")
    @PostMapping("update")
    @ResponseBody
    public Results update(Files file, Integer roleId){
        try{

            file.setUpdateTime(new Date());
            filesService.saveOrUpdate(file);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @ApiOperation("文件删除接口--多个")
    @PostMapping("delAll")
    @ResponseBody
    public Results delAll(String ids){
        try{
            for(String id : ids.split(","))
                filesService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

}
